/*  
* Copyright 2013-2020 Smartdot Technologies Co., Ltd. All rights reserved.  * SMARTDOT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.  * 
*/ package com.ssm.zhou.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;

import com.ssm.zhou.dto.LoginRespUser;

/** 
 * <p>  
 *   
 * @author <a href="mailto:zhoujb@smartdot.com.cn">周竞博</a>  * @version 1.0, 2020年5月31日 
 */
public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		HttpSession session = request.getSession();
		String requestURI = request.getRequestURI();
		LoginRespUser userInfo = (LoginRespUser) session.getAttribute("userInfo");
		// 如果不是登陆页面，进行判断 session中是否有用户信息， 无则请求转发到 login
		if(!requestURI.contains("/login") && !requestURI.contains("/registered") ){
			if( userInfo== null || userInfo.getUserName()==null || userInfo.getUserName().trim().isEmpty()){
				// 重定向到登陆页
				System.out.println("拦截器----session没有用户信息");
				response.sendRedirect(request.getContextPath() +  "/");
				return false;
			}
		}
		return true;
	}

}
 